package com.canbot.u05.activity.presidemode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;


import com.canbot.u05.R;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.MyControlLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * 选择移动方向
 */
public class PresideMoveActivity extends BaseCompereActivity {

        @BindView(R.id.preside_cancel_iv)
        ImageView presideCancelIv;

        @BindView(R.id.preside_sure_iv)
        ImageView presideSureIv;

        @BindView(R.id.control_mcl)
        MyControlLayout controlMcl;

        private int currentIndex = 0;

        private String currentText;

        private String direct;

        private String num;

        private String speed;

        public static final int REQ_MOVE_SET = 9527;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_move_choose);
                ButterKnife.bind(this);
                initView();
        }

        private void initView() {
                Intent intent = getIntent();
                direct = intent.getStringExtra("direct");
                num = intent.getStringExtra("num");
                speed = intent.getStringExtra("speed");
                controlMcl.setMyControlListener(new MyControlLayout.IMyControlListener() {
                        @Override
                        public void onDown(int index) {

                        }

                        @Override
                        public void onPress(int index) {

                        }

                        @Override
                        public void onUp(int index, View childAt) {
                                currentIndex = index;
                                childAt.setSelected(true);
                                startChooseActivityResult();
                        }
                });
        }

        @Override
        protected void onResume() {
                super.onResume();
                initData();
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
        }

        private void initData() {
                if (TextUtils.isEmpty(direct)) {
                        return;
                }
                switch (direct) {
                        case "前进":
                                controlMcl.setIndexChild(0);
                                break;
                        case "右转":
                                controlMcl.setIndexChild(1);
                                break;
                        case "后退":
                                controlMcl.setIndexChild(2);
                                break;
                        case "左转":
                                controlMcl.setIndexChild(3);
                                break;
                        default:
                                controlMcl.setIndexChild(0);
                                break;

                }
        }

        private void startChooseActivityResult() {
                Intent intent = new Intent(this, PresideMoveSetting.class);
                switch (currentIndex) {
                        case 0:
                                currentText = "前进";
                                break;
                        case 1:
                                currentText = "右转";
                                break;
                        case 2:
                                currentText = "后退";
                                break;
                        case 3:
                                currentText = "左转";
                                break;
                }
                intent.putExtra("direct", currentText);
                intent.putExtra("num", num);
                intent.putExtra("speed", speed);
                startActivityForResult(intent, REQ_MOVE_SET);
        }

        @OnClick({R.id.preside_cancel_iv, R.id.preside_sure_iv})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.preside_cancel_iv:
                                finish();
                                break;
                        case R.id.preside_sure_iv:
                                if (TextUtils.isEmpty(speed) || TextUtils.isEmpty(num) || TextUtils.isEmpty(direct)) {
                                        ToastTools.newInstance(this).showText(getString(R.string.data_cannot_empty));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("direct", direct);
                                intent.putExtra("num", num);
                                intent.putExtra("speed", speed);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == RESULT_OK && requestCode == REQ_MOVE_SET) {
                        direct = currentText;
                        num = data.getStringExtra("num");
                        speed = data.getStringExtra("speed");
                }
        }
}
